﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public Canvas can;
    public static Player instance;
    public bool IsMove;
    public int Gold, LineNum;
    public float t1;

    private void Awake()
    {
        instance = this;
        IsMove = true;
       



    }

    private void Start()
    {

        LineNum = MainPanel.instance.LineNum;
        Debug.Log("LineNum===" + LineNum);

    }

    public void GetGoldData(int a)
    {
        Gold = a;
    }




    void Update()
    {
        Vector2 pos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(can.transform as RectTransform, Input.mousePosition, null, out pos);
        if (!IsMove)
        {
            if (!GamePanel.instance.IsDeath)
            {
                if (Input.GetMouseButton(0))
                {
                    if (pos.x >= -481f && pos.x <= 481f)
                    {
                        transform.localPosition = new Vector2(pos.x, transform.localPosition.y);
                    }
                    t1 += Time.fixedDeltaTime;
                    if (t1 > 0.08f)
                    {
                        if (LineNum == 1)
                        {
                            GameObject obj = GameTools.instance.GetGameobject(Balloon.Bullet);
                            obj.transform.SetParent(can.transform);
                            obj.transform.localPosition = transform.localPosition + new Vector3(0, 100, 0);
                        }
                        else if (LineNum > 1)
                        {
                            if (LineNum % 2 == 0)
                            {
                                for (int i = -(LineNum / 2); i <= (LineNum / 2); i++)
                                {
                                    if (i == 0)
                                    {
                                        continue;
                                    }
                                    GameObject obj = GameTools.instance.GetGameobject(Balloon.Bullet);
                                    obj.transform.SetParent(can.transform);
                                    obj.transform.localPosition = transform.localPosition + new Vector3(35 * i, 100, 0);

                                }
                            }
                            else
                            {
                                for (int i = -(LineNum / 2); i <= (LineNum / 2); i++)
                                {

                                    GameObject obj = GameTools.instance.GetGameobject(Balloon.Bullet);
                                    obj.transform.SetParent(can.transform);
                                    obj.transform.localPosition = transform.localPosition + new Vector3(35 * i, 100, 0);

                                }
                            }
                        }
                        else
                        {
                            Debug.Log("############" + LineNum);
                        }
                        //GameObject obj = GameTools.instance.GetGameobject(Balloon.Bullet);
                        //obj.transform.SetParent(can.transform);
                        //obj.transform.localPosition = transform.localPosition + new Vector3(0, 100, 0);


                        t1 = 0;
                    }


                }
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!collision.gameObject.tag.Equals("Bullet"))
        {
            GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/ShiBaiPanel"));
            obj.transform.SetParent(can.transform);
            obj.transform.localPosition = Vector3.zero;
            GamePanel.instance.IsDeath = true;
        }



    }

}
